<?php

namespace App\Admin\Controllers;

use App\Models\Config;
use App\Models\Good;
use App\Models\Supplier;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Form\Field\Table;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GoodController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Good';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Good());
        $grid->model()->orderBy('id', 'desc');

        $grid->disableExport();
        $cate = \request('cate');

        $type =  DB::table('type')->where('type',$cate)->pluck('name','id')->toArray();
        $sup =  DB::table('supplier')->pluck('name','id')->toArray();

        $grid->filter(function($filter) use ($type,$sup){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();


            $filter->column(1/2,function ($filter) use ($type){
                $filter->like('name','商品名称');



            $filter->equal('cate', '商品类型')->select(['1' => '开蚌', '2' => '饰品','3'=>'成品'])->load('type_id','/admin/getType');
//
//
//            $filter->equal('type_id', '商品分类')->select(function () {
//                $cate = request('q');
//
//                return DB::table('type')->where('type',$cate)->pluck('name','id')->toArray();
//
//            });
                $filter->equal('type_id','商品分类')->select($type);
            });
            $filter->column(1/2,function ($filter) use ($sup){
                $filter->like('specs','规格型号');
                $filter->like('No','商品货号');
                $filter->equal('supplier_id','供应商')->select($sup);
            });


        });

        $grid->column('id', __('Id'));
        $grid->column('name', __('商品名称'));
            //->expand(function ($model) {

//            $comments = $model->comments()->take(10)->get()->map(function ($comment) {
//                return $comment->only(['id', 'content', 'created_at']);
//            });
//
//            return new Table(['ID', '公司', '价格'], $comments->toArray());
       // });

        $grid->column('cate', __('商品类型'))->using(['1' => '开蚌', '2' => '饰品','3'=>'成品']);

        $type1 =  DB::table('type')->pluck('name','id')->toArray();
        $grid->column('type_id', __('商品分类'))->using($type1);
        $grid->column('color.color', __('商品颜色'));
        $grid->column('No', __('商品货号'));
        $grid->column('specs', __('规格型号'));
        $grid->column('img', __('商品图片'))->lightbox(['width' => 50, 'height' => 50]);
        //$grid->column('detail_img', __('详情'));
        //$grid->column('old_price', __('Old price'));
        $grid->column('old_price', __('原价'));
        $grid->column('new_price', __('优惠价'));
        $grid->column('cost_price', __('成本价'));
        $grid->column('stock','库存')->editable();

        $grid->column('created_at', __('创建时间'));
        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'primary'],
            'off' => ['value' => 2, 'text' => '禁用', 'color' => 'default'],
        ];
        $grid->column('status','禁用状态')->switch($states);
        //$grid->column('updated_at', __('Updated at'));

        $grid->actions(function($actions){

            $actions->disableView();
            $actions->disableDelete();

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Good::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('商品名称'));
        $show->field('product', __('规格属性'));
        $show->field('description', __('商品简介'));
        $show->field('img', __('商品图片'))->image();
        $show->field('old_price', __('原价'));
        $show->field('new_price', __('优惠价'));
        $show->field('cost_price', __('成本价'));
        $show->field('stock', __('库存'));
        $show->field('detail_img', __('详情'))->unescape();
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Good());

        $color = DB::table('color')->pluck('color','id');
        $supplier =  DB::table('supplier')->pluck('name','id');
        //$type =  DB::table('type')->pluck('name','id');
        $material =  DB::table('material')->pluck('material','id');

        $form->text('name', __('商品名称'))->required();
        $form->text('model', __('商品型号'));
        $form->text('specs', __('商品规格'))->required();;
        $form->text('No', __('商品货号'));
        $form->select('cate', __('商品类型'))->options(['1' => '开蚌', '2' => '饰品','3'=>'成品'])->when(1, function (Form $form) {

            $type =  DB::table('type')->where('type',1)->pluck('name','id');
            $form->select('type_id', __('商品分类'))->options($type);
        })->when(2, function (Form $form) {

            $type =  DB::table('type')->where('type',2)->pluck('name','id');

            $form->select('type_id', __('商品分类'))->options($type);
        })->when(3, function (Form $form) {

            $type =  DB::table('type')->where('type',3)->pluck('name','id');

            $form->select('type_id', __('商品分类'))->options($type);
        })->required();

        $form->select('color_id', __('商品颜色'))->options($color)->required();
        $form->select('material_id', __('商品材质'))->options($material)->required();
        $form->select('supplier_id', __('供应商'))->options($supplier)->required();
//
        $form->multipleFile('img', __('商品图片'))->removable()->options([
            'dropZoneEnabled' => true, // 该参数允许拖拽上传
            'browseOnZoneClick' => true, // 该参数允许点击上传
            'slugCallback' => false, // 该参数是重新选择后依旧保留之前的，并且不会重复显示
            'uploadUrl' => '#', // 异步上传
            'showUpload' => false, // 是否显示上传按钮
            'layoutTemplates' => ['actionUpload' => ''], // 该参数要与uploadUrl结合使用，目的是为了不是异步上传的，但是能删掉多张图片中的某一张
            'maxFileCount' => 10 // 该参数是最多只能选择多少张
        ]);
;

        $form->text('cost_price', __('成本价'))->required();;
        $form->text('old_price', __('原价'))->required();;
        $form->text('new_price', __('优惠价'))->required();;
        //$form->decimal('old_price', __('Old price'));
        //$form->text('nable', __('标签'));
        $form->text('stock','库存')->required();
        //$form->UEditor('detail_img','商品详情')->options(['initialFrameHeight' => 400]);
//        if(request()->isMethod('post')){
//            dd(request()->all());
//        }
        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            'off' => ['value' => 2, 'text' => '禁用', 'color' => 'danger'],
        ];

        $form->switch('status', __('状态'))->states($states)->default('1');
        $form->footer(function ($footer) {

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });


        //保存前回调
        $form->saving(function (Form $form) {
            if(!$form->No){

                $supplier = Supplier::find($form->supplier_id);
                if($supplier)
                     $form->No = $supplier->number.'-'.rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);

            }
        });

        return $form;
    }




}
